
//var FFWD_RX_SimpleField=/^\[([a-zA-Z0-9_]*)\]\[([a-zA-Z0-9_]*)\]\[([a-zA-Z0-9_]*)\]/;
var FFWD_RX_JoinedField=/^\[([a-zA-Z0-9_]*)\]\[\[([a-zA-Z0-9_]*)\]\[([a-zA-Z0-9_]*)\]\[([a-zA-Z0-9_]*)\]\]\[([a-zA-Z0-9_]*)\]/;



function FFWD_ActiveRecord(){
	this.fields=[];
	this.uid=null;
	
	this.getUID=function(){return this.uid;}
	
	this.getData=function(){
		var r=[];
		for(i in this.fields)r[$(this.fields[i]).attr('pointer')]=$(this.fields[i]).attr('value');	
		return r;
	}
	
	this.getFieldByPointer=function(ptr){
		for(i in this.fields)if($(this.fields[i]).attr('pointer')==ptr)return this.fields[i];
		return null;
	}
	
	this.getFieldsByLocalPointer=function(ptr){
		var r=[];
		for(i in this.fields)if($(this.fields[i]).attr('localPointer')==ptr)r.push(this.fields[i]);
		return r;
	}
	
	this.push=function(obj){
		if(!this.uid)this.uid=$(obj).attr('uid');
		a=FFWD_RX_JoinedField.exec($(obj).attr('pointer'));
		if(a)$(obj).attr('localPointer','['+a[2]+']['+a[3]+']['+a[4]+']');
		this.fields.push(obj);		
	}
	
	this.onLocalChange=function(e){
		myRecord=e.data.record;		
		localValue=$(this).attr('value');
		myForeigns=myRecord.getFieldsByLocalPointer($(this).attr('pointer'));
		$(myForeigns).attr('value','your\'s local changed to '+localValue);		
	}
	
	this.onForeignChange=function(e){
		myRecord=e.data.record;
		myLocal=myRecord.getFieldByPointer($(this).attr('localPointer'));
		foreignValue=$(this).attr('value');		
		$(myLocal).attr('value','your\'s foreign changed to '+foreignValue)
	}
	
	this.onForeignClick=function(e){
		myRecord=e.data.record;
		myLocal=myRecord.getFieldByPointer($(this).attr('localPointer'));
		foreignValue=$(this).attr('value');		
		$(myLocal).attr('value','your\'s foreign is clicked and changed to '+foreignValue);
		var selectbox=FFWD_ARH.OpenSelect(this);
		
		$(this).unbind('blur.ffwd_ar').bind('blur',function(){
			console_log('blur');
			$(selectbox).detach();
		});
	}
	
	this.onLoaded=function(){
		for(i in this.fields){
			if($(this.fields[i]).attr('localPointer')){
				
				$(this.fields[i]).unbind('.ffwd_ar').
					bind('change.ffwd_ar keyup.ffwd_ar',{record:this},this.onForeignChange).
					bind('click.ffwd_ar',{record:this},this.onForeignClick);
				$(this.getFieldByPointer($(this.fields[i]).attr('localPointer'))).unbind('.ffwd_ar').
					bind('change.ffwd_ar keyup.ffwd_ar',{record:this},this.onLocalChange);
			};
		};		
	};	
}

function FFWD_ActiveRecordHandler(){	
	var records={};
	var selectInput=null;
	
	this.OpenSelect=function(input){   // <<- Külön osztályba szedni!
		if(!selectInput){
			selectInput=document.createElement('div');
			selectInput.style.position='absolute';
			selectInput.style.width='100px';
			selectInput.style.height='100px';
			selectInput.style.backgroundColor='#fff';
			selectInput.style.color='#000';	
			selectInput.style.border='1px solid #aaa';
		}else{
			$(selectInput).detach();			
		}
		selectInput.style.width=($(input).width()-16)+'px';
		selectInput.style.marginTop=($(input).height()+3)+'px';
		$(input).before(selectInput);
		$(selectInput).show();
		return selectInput;
	}

	this.ScanForRecords=function(){
		$(':input[uid]').each(function(){
			uid=$(this).attr('uid');
			if(!records[uid])records[uid]=new FFWD_ActiveRecord();
			records[uid].push(this);
		});		
		for(i in records)records[i].onLoaded();
	};
	
	this.dumpAll=function(){
		var r=[];
		for(i in records)r[records[i].getUID()]=records[i].getData();
		console_log(r);
	}
	
	this.ScanForRecords();	
	for(i in records)console_log(records[i]);
};



var FFWD_ARH=0;

$(document).ready(function(){
	FFWD_ARH=new FFWD_ActiveRecordHandler();
});